import { computed, ComputedRef, ref } from 'vue'
import { useStore } from 'vuex'

interface LoadPrams {
  currentPage: number;
  pageSize: number;
}
const useLoadMore = (
  actionName: string,
  total: ComputedRef,
  params: LoadPrams = {
    currentPage: 2,
    pageSize: 2
  }) => {
  const store = useStore()
  const currentPage = ref(params.currentPage)
  const requestParams = computed(() => {
    return {
      current: currentPage.value,
      size: params.pageSize
    }
  })

  const loadMorePage = () => {
    store.dispatch(actionName, requestParams.value).then(res => {
      currentPage.value++
    })
  }

  const isLastpage = computed(() => {
    return Math.ceil(total.value / params.pageSize) < currentPage.value
  })
  return {
    loadMorePage,
    isLastpage,
    currentPage,
  }
}

export default useLoadMore
